package com.ucode.application.security;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ucode.springboot.starter.security.entity.UcodeGrantedAuthority;
import com.ucode.springboot.starter.security.entity.UcodeUser;
import com.ucode.springboot.starter.security.service.UcodeUserService;
import com.ucode.sys.model.Role;
import com.ucode.sys.model.RoleMenu;
import com.ucode.sys.model.User;
import com.ucode.sys.service.RoleService;
import com.ucode.sys.service.UserService;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;

@Service
public class UcodeUserServiceImpl implements UcodeUserService {
    
    @Autowired
    private UserService userService;
    @Autowired
    public RoleService roleService;
    
    @Override
    public UcodeUser getUser(String username) throws RuntimeException {
        User user = userService.findByMEA(username);
        UcodeUser securityUser = null;
        if(user != null && user.getId() != null){
            /**
             * 账号是否未过期
             */
            boolean accountNonExpired = true;
            /**
             * 账号是否未锁定
             */
            boolean accountNonLocked = true;
            /**
             * 密码是否未过期
             */
            boolean credentialsNonExpired = true;
            
            Set<String> roles = null;
            List<UcodeGrantedAuthority> authoritys = null;
            
            List<Role> roleObjs = roleService.findRolesByUserId(user.getId());
            if(CollUtil.isNotEmpty(roleObjs)){
                roles = new HashSet<String>();
                authoritys = new ArrayList<UcodeGrantedAuthority>();
                List<Long> roleIds = new ArrayList<Long>();
                for (Role role : roleObjs) {
                    if(StrUtil.isNotBlank(role.getCode())){
                        roles.add(role.getCode());
                        authoritys.add(new UcodeGrantedAuthority("ROLE_"+role.getCode()));
                    }
                    roleIds.add(role.getId());
                }
                
                List<RoleMenu> permissions = roleService.findListByRoleIds(roleIds);
                if(CollUtil.isNotEmpty(permissions)){
                    for(RoleMenu roleMenu : permissions){
                        if(StrUtil.isNotBlank(roleMenu.getPermission()) && roles.add(roleMenu.getPermission())){
                            authoritys.add(new UcodeGrantedAuthority(roleMenu.getPermission()));
                        }
                    }
                }
            }
            
            securityUser = new UcodeUser(user.getId().toString(),username, user.getPassword(), accountNonExpired, accountNonLocked, credentialsNonExpired, roles,authoritys);
        }
        return securityUser;
    }

}
